package com.hspedu.newData.dmsxl.回溯.组合;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC17电话号码的字母组合 {

    StringBuilder str = new StringBuilder();
    List<String> res = new ArrayList<>();
    final HashMap<Character,String> map = new HashMap<>();
    {
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");
    }

    public List<String> letterCombinations(String digits) {
        if (digits.equals("")) return res;
        backtracking(digits,digits.length(),0);
        return res;
    }

    public void backtracking(String digits,int len,int startIndex){
        if (str.length() == len){
            res.add(str.toString());
            return;
        }

        String s = map.get(digits.charAt(startIndex));
        for (int i = 0; i < s.length(); i++) {
            str.append(s.charAt(i));
            backtracking(digits,len,startIndex + 1);
            str.deleteCharAt(str.length() - 1);
        }
    }

    @Test
    public void test() {
        // 测试代码
        List<String> list = letterCombinations("2");
        System.out.println("=========");
    }
}
